/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Jun 1, 2012  10:54:36 AM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : apps.business.process.construction.contract
 * File Name : ContractContractorLowRiseProcessing.java
 * TODO
 */
package apps.business.process.construction.contract;

import java.util.List;

import jpa.entitys.promis.construction.SyncPrBudget;
import jpa.entitys.promis.construction.VwSyncContract;
import jpa.entitys.synchro.staging.MmPocontractorOutb;
import jpa.entitys.synchro.staging.MmPurchaseRequest;
import org.apache.log4j.Logger;
import promis.utility.CommonService;
import sap.business.mm.outbound.SyncPrItemService;
import sap.business.mm.outbound.VwSyncContractService;
import apps.business.process.SyncProcessorCommons;
import apps.promis.out.common.PurchaseRequestAction;
import apps.promis.out.construction.VwSyncPoContractorAction;

/**
 * @author chatree
 *
 */
public class ContractContractorLowRiseProcessing extends SyncProcessorCommons {
	
	private static final Logger log = Logger.getLogger(ContractContractorLowRiseProcessing.class);

	/**
	 * 
	 */
	public ContractContractorLowRiseProcessing() {
		// TODO Auto-generated constructor stub
	}
	
	/**
	 * Construction from VO 
	 * if VO type include Purchase Request (PR)
	 * @param company
	 * @param project
	 * @param documentNo
	 * @param status
	 * @param syncIsstaging
	 * @throws Exception
	 */
	public void ProcessingContarct(String company, String project, String documentNo, String status, String syncIsstaging) throws Exception {
		System.out.println("Start ProcessingContarct  ProcessingContarct  --->");
		try{
			System.out.println("company = " + company + " project = " + project + " documentNo = " + documentNo + "  syncIsstaging = " + syncIsstaging + " status = " + status);
			UPD_PROMISUseTime(true, false, null);
			VwSyncPoContractorAction obb = new VwSyncPoContractorAction();			
			List<MmPocontractorOutb> value_list = obb.CreatetoStaging(company, project, documentNo, status, syncIsstaging);
			UPD_PROMISUseTime(false, true, null);			
			
			if(CommonService.getSize(value_list) > 0){
				UPD_SENDUseTime(true, false, value_list.get(0).getInstanceNo());
				new VwSyncContractService().SendToSAP(value_list);
				UPD_SENDUseTime(false, true, value_list.get(0).getInstanceNo());
			}
			System.out.println("Sending PO Contract To Staging size="+ CommonService.getSize(value_list) +"  --->");
			
			//find by Vo Docuemnt
			List<SyncPrBudget> lstBudget = new PurchaseRequestAction().FindByVODocumentNo(company, project, documentNo, status);
			
			if(!CommonService.IsNull(lstBudget)){			
				SyncPrBudget budget = lstBudget.get(0);
				if(budget.getRefType().equals("VO")){
					documentNo = budget.getDocumentNo(); //log.info("PR DocumentNo : " + documentNo);
					
					//CreateTo Staging
					List<MmPurchaseRequest> pr_value_list = new PurchaseRequestAction().CreateToStaging(company, project, documentNo, status, syncIsstaging);
					System.out.println("Create Purchase Request To Staging size="+ CommonService.getSize(pr_value_list) +"  --->");
					
					//Send to SAP
					SyncPrItemService SyncPrItemServ = new SyncPrItemService();
					SyncPrItemServ.SendingToSAP(pr_value_list);
					System.out.println("Sending Purchase Request To SAP size="+ CommonService.getSize(pr_value_list) +"  --->");
				}
			}
		}catch(Exception e){
			e.printStackTrace();
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
	}
	
	
	public void CancelContract(String company, String project, String documentNo, String status, String syncIsstaging) throws Exception {
		try{
			
			UPD_PROMISUseTime(true, false, null);
			VwSyncPoContractorAction obb = new VwSyncPoContractorAction();
			List<MmPocontractorOutb> value_list = obb.CreatetoStaging(company, project, documentNo, status, syncIsstaging);
			UPD_PROMISUseTime(false, true, null);
			System.out.println("Create to Staging  size="+ CommonService.getSize(value_list) +"  --->");
			obb = null;
			
			if(CommonService.getSize(value_list) > 0){
				UPD_SENDUseTime(true, false, value_list.get(0).getInstanceNo());
				new VwSyncContractService().SendToSAP(value_list);
				UPD_SENDUseTime(false, true, value_list.get(0).getInstanceNo());
			}			
			System.out.println("Sended to SAP  size="+ CommonService.getSize(value_list) +"  --->");
			
		}catch(Exception e ){
			e.printStackTrace();
			e.printStackTrace();
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
	}

	@Deprecated
	private void Start() throws Exception {
		try{
			
			VwSyncPoContractorAction obb = new VwSyncPoContractorAction();
			String company = "SP"; 
			String project = "VP";  //SPLVILLE-PRAGSA 
			String documentNo = "CT-1207-000051";
			//String documentNo = "VO-1206-000006";
			String status = "A";
			String syncIsstaging = "N";
			
			List<MmPocontractorOutb> value_list = obb.CreatetoStaging(company, project, documentNo, status, syncIsstaging);
			log.info(" Create to Staging. ");
			
			new VwSyncContractService().SendToSAP(value_list);
			log.info(" Send to SAP. ");
			
			//find by Vo Docuemnt
			PurchaseRequestAction PurchaseRequestAct = new PurchaseRequestAction();
			List<SyncPrBudget> lstBudget = PurchaseRequestAct.FindByVODocumentNo(company, project, documentNo, status);
			
			if(!CommonService.IsNull(lstBudget)){			
				SyncPrBudget budget = lstBudget.get(0);
				if(budget.getRefType().equals("VO")){
					documentNo = budget.getDocumentNo(); 				log.info("PR DocumentNo : " + documentNo);
					
					//CreateTo Staging
					log.info("Start Create Purchase Request To Staging =======================================>");
					PurchaseRequestAction purchaseRequestAction = new PurchaseRequestAction();
					List<MmPurchaseRequest> pr_value_list = purchaseRequestAction.CreateToStaging(company, project, documentNo, status, syncIsstaging);
					log.info("End Create Purchase Request To Staging =======================================>");
					
					//Send to SAP
					log.info("Start Sending Purchase Request To SAP =======================================>");
					SyncPrItemService SyncPrItemServ = new SyncPrItemService();
					SyncPrItemServ.SendingToSAP(pr_value_list);
					log.info("End Sending Purchase Request To SAP =======================================>");
				}
			}			
			
		}catch(Exception e){
			e.printStackTrace();
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
	}
	
	@Deprecated
	private void ReRunAll() throws Exception {
		try{
			
			String company ="SP";
			String project = "VSK";
			
			List<VwSyncContract> lst = new VwSyncPoContractorAction().ReRunAll(company, project);
			if(!CommonService.IsNull(lst)){
				
				for(VwSyncContract value : lst)				
					ProcessingContarct(company, project, value.getDocumentNo(), value.getStatus(), value.getSyncIsstaging());
			}
			
		}catch(Exception e){
			e.printStackTrace();
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		try{
			
			new ContractContractorLowRiseProcessing().Start();
			
		}catch(Exception e){
			e.printStackTrace();
			log.error(e.getMessage());
			System.err.println(e.getMessage());
		}

	}

}